package com.hzbank.swancs.view.menu;

import static com.hzbank.swancs.utils.SharedPreferencesUtil.SPKey.ASSISTFLAG;
import static com.hzbank.swancs.utils.SharedPreferencesUtil.SPKey.OPER_NO;
import static com.hzbank.swancs.utils.SharedPreferencesUtil.SPKey.ORGAN_CODE;

import android.os.Handler;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzbank.swancs.fragment.remote.viewmodel.OkHttpClient;
import com.hzbank.swancs.mtop.WeexRequestBean;
import com.hzbank.swancs.utils.HLogger;
import com.hzbank.swancs.utils.ReflectUtils;
import com.hzbank.swancs.utils.ReflectionApplication;
import com.hzbank.swancs.utils.SharedPreferencesUtil;
import com.hzbank.swancs.utils.ThreadUtils;
import com.hzbank.swancs.utils.TimeUtil;
import com.hzbank.swancs.utils.ToastUtils;
import com.hzbank.swancs.utils.WriteLogUtil;
import com.hzbank.swancs.view.head.MTopUtils;
import com.hzbank.swancs.view.head.SwancsHeadNetUtils;
import com.hzbank.swancs.view.menu.adapter.SwancsSystemNormalAdapter;
import com.hzbank.swancs.view.menu.bean.SimpleSystemMenuBean;
import com.hzbank.swancs.view.menu.bean.SwancsSystemBean;
import com.hzbank.swancs.view.menu.bean.SwancsSystemCommonSimpleBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @类名: SwancsSystemMenuManager
 * @描述:
 * @作者：fangkaijin
 * @邮箱: fangkaijin@outlook.com
 * @时间: 2023/3/8
 **/
public class SwancsSystemMenuManager {

    static volatile SwancsSystemMenuManager defaultInstance;

    public static SwancsSystemMenuManager getDefault(){

        SwancsSystemMenuManager instant = defaultInstance;

        if(null == instant){

            synchronized (SwancsSystemMenuManager.class){

                instant = SwancsSystemMenuManager.defaultInstance;

                if(null == instant){

                    instant = SwancsSystemMenuManager.defaultInstance = new SwancsSystemMenuManager();
                }
            }
        }

        return instant;

    }

    private static SwancsSystemBean allCustomSwancsSystem;
    private static SwancsSystemBean allInnerSwancsSystem;

    private static boolean innerMenuCanUsed = false;

    private static boolean commonMenuCanUsed = false;

    private LinkedList<SimpleSystemMenuBean> simpleSystemMenus;
    private WxSwancsSystemMenuView swancsSystemMenuView;


    private static List<SwancsSystemCommonSimpleBean> collectSystems = new LinkedList<>(); //常用菜单

    private Handler mHandler = new Handler();

    public void destoryMenuView(){

        if(null!=mHandler){

            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }

        allInnerSwancsSystem = null;
        allCustomSwancsSystem = null;
        if(null!=collectSystems){
            collectSystems.clear();
            collectSystems = null;
        }
    }

    //assistFlag = "1" 辅助系统
    //assistFlag  其他 非辅助，旧逻辑
    public void requestAllSystemMenu(String assistFlag, RequestSysmenuCompleteCallback requestSysmenuComplete){

        if(null != simpleSystemMenus){

            simpleSystemMenus.clear();
            simpleSystemMenus = null;
        }

        if(TextUtils.equals("1", assistFlag)){


            requestSystemMenu(1, new RequestSysmenuCompleteCallback() {
                @Override
                public void loadComplete(int menuType) {

                    if(null!=requestSysmenuComplete) requestSysmenuComplete.loadComplete(1);
                }

                @Override
                public void loadSimpleSystemMenu(String simpleMenuJson) {
                    //if(null!=requestSysmenuComplete) requestSysmenuComplete.loadSimpleSystemMenu(simpleMenuJson);
                }
            });

        } else {

            requestSystemMenu(2, new RequestSysmenuCompleteCallback() {
                @Override
                public void loadComplete(int menuType) {

                    //对客菜单请求完毕回调
                    if(null!=requestSysmenuComplete) requestSysmenuComplete.loadComplete(2);

                    ThreadUtils.postUI(() -> {

                        if(null == mHandler){
                            mHandler = new Handler();
                        }

                        mHandler.postDelayed(() -> {

                            requestSystemMenu(1, requestSysmenuComplete);
                        }, 1500);
                    });
                }

                @Override
                public void loadSimpleSystemMenu(String simpleMenuJson) {
                    if(null!=requestSysmenuComplete) requestSysmenuComplete.loadSimpleSystemMenu(simpleMenuJson);
                }
            });

        }

    }
    
    //调用接口，请求系统菜单
    //menuType 1-对内 2-对客  默认展示对客
    private void requestSystemMenu(int menuType, RequestSysmenuCompleteCallback sysmenuComplete){

        //请求接口
        //new SwancsHeadNetUtils.NetBuilder().setNetName("queueingMachineRecallNumber").setParams(params)

        if(1 == menuType){

            HLogger.logWithTag("SystemMenuCustomer", "搜索菜单数据-对内菜单数据开始请求");
        } else if(2 == menuType){

            HLogger.logWithTag("SystemMenuCustomer", "搜索菜单数据-对客菜单数据开始请求");
        }

        WeexRequestBean weexRequestBean = new WeexRequestBean();
        weexRequestBean.setApiName("emas.swan.home.menu");
        weexRequestBean.setRequestMethod("POST");
        weexRequestBean.setVersion("1.0");

//        {
//            "parameters": {
//            "menu_flag": "0"
//        },
//            "permType": "PPPP0001",
//                "operCode": [
//            "111",
//                    "222"
//    ],
//            "menuFlag": 1,
//                "branchNo": "7571",
//                "operId": "311228",
//                "operatorId": "311228",
//                "operatorNo": "311228",
//                "systemCode": "SWANCS"
//        }
        String assistFlag= (String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), ASSISTFLAG, "");
        String systemMenuParams="";
        if("1".equals(assistFlag)){
            systemMenuParams = "{\"parameters\":{\"menu_flag\":\"0\"}," +
                    "\"permType\":\"PPPP0001\"," +
                    "\"operCode\":[\"111\",\"222\"]," +
                    "\"menuFlag\":"+menuType+"," +
                    "\"assistFlag\":"+assistFlag+"," +
                    "\"branchNo\":\""+(String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), ORGAN_CODE, "")+"\"," +
                    "\"operId\":\""+(String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), SharedPreferencesUtil.SPKey.OPER_NO, "")+"\"," +
                    "\"operatorId\":\""+(String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), SharedPreferencesUtil.SPKey.OPER_NO, "")+"\"," +
                    "\"operatorNo\":\""+(String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), SharedPreferencesUtil.SPKey.OPER_NO, "")+"\"," +
                    "\"systemCode\":\"SWANCS\"}";

        }else{
            systemMenuParams = "{\"parameters\":{\"menu_flag\":\"0\"}," +
                    "\"permType\":\"PPPP0001\"," +
                    "\"operCode\":[\"111\",\"222\"]," +
                    "\"menuFlag\":"+menuType+"," +
                    "\"branchNo\":\""+(String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), ORGAN_CODE, "")+"\"," +
                    "\"operId\":\""+(String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), SharedPreferencesUtil.SPKey.OPER_NO, "")+"\"," +
                    "\"operatorId\":\""+(String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), SharedPreferencesUtil.SPKey.OPER_NO, "")+"\"," +
                    "\"operatorNo\":\""+(String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), SharedPreferencesUtil.SPKey.OPER_NO, "")+"\"," +
                    "\"systemCode\":\"SWANCS\"}";
        }

        try{

            new SwancsHeadNetUtils.NetBuilder().
                    setIsJsonParam(true).
                    setParamsJson(JSONObject.parseObject(systemMenuParams)).
                    setGetWayParams(weexRequestBean)
                    .setNetCallback(new OkHttpClient.OkCallCancelBack() {
                        @Override
                        public void fail(JSONObject error) {

                            HLogger.logWithTag("SystemMenuCustomer", "菜单数据获取失败-menutype="+menuType);

                            WriteLogUtil.getInstance().saveSearchMenu("菜单数据获取失败-menutype="+menuType+"错误信息="+((null == error) ? "" :JSON.toJSONString(error)));

                            if(null != sysmenuComplete){
                                sysmenuComplete.loadComplete(-1);
                            }

                            if(1 == menuType){

                                //通知对内菜单封装完成

                                innerMenuCanUsed = true;
                            }
                        }

                        @Override
                        public void success(JSONObject data) {

                            if(null!=data){

                                //解析
                                if(1 == menuType){

                                    allInnerSwancsSystem = JSON.parseObject(JSON.toJSONString(data), SwancsSystemBean.class);

                                    WriteLogUtil.getInstance().saveSearchMenu("菜单搜索-对内部分菜单开始解析");
                                    parseSimpleSystemMenu(allInnerSwancsSystem);
                                    HLogger.logWithTag("SystemMenuCustomer", "搜索菜单数据-对内部分解析完成");

                                    List<String> shouldDeleteMenus = new ArrayList<>();
                                    deleteInvalidMenu(allInnerSwancsSystem, shouldDeleteMenus);
                                    //deleteInvalidMenu(allInnerSwancsSystem);

                                    if(null!=shouldDeleteMenus && !shouldDeleteMenus.isEmpty()){
                                        deleteInvalidParentMenu(allInnerSwancsSystem, shouldDeleteMenus);
                                    }

                                    if(null != sysmenuComplete && TextUtils.equals("1", assistFlag)){
                                        sysmenuComplete.loadComplete(1);
                                    }

                                    if(null != sysmenuComplete){

                                        WriteLogUtil.getInstance().saveSearchMenu("搜索菜单数据-解析完成，数量="+((null == simpleSystemMenus)? 0 : simpleSystemMenus.size()));
                                        sysmenuComplete.loadSimpleSystemMenu((null == simpleSystemMenus || simpleSystemMenus.isEmpty()) ? "" :JSON.toJSONString(simpleSystemMenus));
                                    }

                                    WriteLogUtil.getInstance().saveSystemMenu(ReflectionApplication.getApplication(), menuType, JSON.toJSONString(allInnerSwancsSystem));


                                    //通知对内菜单封装完成

                                    innerMenuCanUsed = true;

                                } else if(2 == menuType) {

                                    allCustomSwancsSystem = JSON.parseObject(JSON.toJSONString(data), SwancsSystemBean.class);

                                    WriteLogUtil.getInstance().saveSearchMenu("菜单搜索-对客部分菜单开始解析");

                                    parseSimpleSystemMenu(allCustomSwancsSystem);
                                    HLogger.logWithTag("SystemMenuCustomer", "搜索菜单数据-对客部分解析完成");

                                    List<String> shouldDeleteMenus = new ArrayList<>();

                                    deleteInvalidMenu(allCustomSwancsSystem, shouldDeleteMenus);
                                    //deleteInvalidMenu(allCustomSwancsSystem);

                                    if(null!=shouldDeleteMenus && !shouldDeleteMenus.isEmpty()){
                                        deleteInvalidParentMenu(allCustomSwancsSystem, shouldDeleteMenus);
                                    }

                                    if(null != sysmenuComplete){
                                        sysmenuComplete.loadComplete(2);
                                    }

                                    WriteLogUtil.getInstance().saveSystemMenu(ReflectionApplication.getApplication(), menuType, JSON.toJSONString(allCustomSwancsSystem));
                                }
                            }
                        }
                    }).build().doRequest();

        } catch (Exception e){

            e.printStackTrace();

            if(1 == menuType){

                //通知对内菜单封装完成

                innerMenuCanUsed = true;
            }
        }

    }

    //获取操作员常用菜单数据
    public void requestCommonMenus(){

        try{

            HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据获取开始执行");

            HashMap params = new HashMap();
            params.put("operNo", (String)SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), OPER_NO, ""));

            WeexRequestBean weexRequestBean = new WeexRequestBean();
            weexRequestBean.setApiName("emas.swan.home.common.getMenuInfo");
            weexRequestBean.setRequestMethod("POST");
            weexRequestBean.setVersion("1.0");


            new SwancsHeadNetUtils.NetBuilder().setIsJsonParam(false).setGetWayParams(weexRequestBean).setParams(params).setCommonParams(MTopUtils.getCommonParams()).setNetCallback(new OkHttpClient.OkCallCancelBack() {
                @Override
                public void fail(JSONObject error) {

                    HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据获取失败");

                    collectSystems = new LinkedList<>();

                    commonMenuCanUsed = true;
                }

                @Override
                public void success(JSONObject data) {


                    if(null!=data){

                        ThreadUtils.post(() -> {

                            try{

                                JSONArray menus = data.getJSONArray("plist");

                                HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据plist="+JSON.toJSONString(menus));

                                WriteLogUtil.getInstance().saveCommonSystemMenuLog("常用菜单数据plist="+JSON.toJSONString(menus) + "\n\r");

                                if(null == menus || menus.isEmpty()){

                                    HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据获取为空");

                                    collectSystems = new LinkedList<>();

                                } else {


                                    collectSystems = new LinkedList<>();

                                    List<SwancsSystemCommonSimpleBean> commonSimples = JSON.parseArray(menus.toJSONString(), SwancsSystemCommonSimpleBean.class);

                                    if(null == commonSimples || commonSimples.isEmpty()){

                                        HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据获取为空");

                                    }else {

                                        //先根据排序子段排序
                                        //List<SwancsSystemCommonSimpleBean> collectSystemSimples = commonSimples.stream().sorted(Comparator.comparing(SwancsSystemCommonSimpleBean::getMenuNo)).collect(Collectors.toList());

                                        //按照实际菜单排序字段排序
                                        Collections.sort(commonSimples, new Comparator<SwancsSystemCommonSimpleBean>() {
                                            @Override
                                            public int compare(SwancsSystemCommonSimpleBean o1, SwancsSystemCommonSimpleBean o2) {

                                                try{

                                                    return Integer.parseInt(o1.menuNum) - Integer.parseInt(o2.menuNum);

                                                }catch (Exception e){
                                                    e.printStackTrace();
                                                }

                                                return 0;
                                            }
                                        });

                                        HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据排序后的顺序="+JSON.toJSONString(commonSimples));

                                        WriteLogUtil.getInstance().saveCommonSystemMenuLog("常用菜单数据排序后的顺序="+JSON.toJSONString(commonSimples) + "\n\r");
                                        collectSystems.addAll(commonSimples);

                                    }
                                }

                            }catch (Exception e){

                                e.printStackTrace();

                                HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据获取失败");

                                collectSystems = new LinkedList<>();
                            }

                        });


                    }else {

                        HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据获取为空");

                        collectSystems = new LinkedList<>();
                    }

                    commonMenuCanUsed = true;


                }
            }).build().doRequest();

        } catch (Exception e){

            e.printStackTrace();

            HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据获取失败");

            collectSystems = new LinkedList<>();
            commonMenuCanUsed = true;
        }
    }

    //新增常用菜单
    private void addNormalMenu(SwancsSystemCommonSimpleBean swancsSystem){

        if(null == swancsSystem || null== collectSystems || collectSystems.isEmpty()) return;

        try{

            HashMap params = new HashMap();

            //菜单码
            params.put("menuNo", TextUtils.isEmpty(swancsSystem.menuNo) ? "" : swancsSystem.menuNo);
            //菜单名称
            params.put("menuName", TextUtils.isEmpty(swancsSystem.menuName) ? "" : swancsSystem.menuName);
            //操作员号
            params.put("operNo", (String)SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), OPER_NO, ""));
            //更新时间
            params.put("updateTime", TimeUtil.formateTimestamp(System.currentTimeMillis(), "yyyy-MM-dd"));
            //菜单排序字段
            params.put("menuNum", collectSystems.size() - 1);

            params.put("parentMenuCode", TextUtils.isEmpty(swancsSystem.parentMenuCode) ? "" : swancsSystem.parentMenuCode);
            params.put("menuUrl", TextUtils.isEmpty(swancsSystem.menuUrl) ? "" : swancsSystem.menuUrl);
            params.put("menuLevel", (0 == swancsSystem.menuLevel) ? -1 :swancsSystem.menuLevel);
            params.put("transType", TextUtils.isEmpty(swancsSystem.transType) ? "" : swancsSystem.transType);
            params.put("module", TextUtils.isEmpty(swancsSystem.module) ? "" : swancsSystem.module);
            params.put("menuSort", TextUtils.isEmpty(swancsSystem.menuSort) ? "" : swancsSystem.menuSort);

            HLogger.logWithTag("SystemMenuCustomer", "新增常用菜单参数="+ JSON.toJSONString(params));

            WeexRequestBean weexRequestBean = new WeexRequestBean();
            weexRequestBean.setApiName("emas.swan.home.common.insertData");
            weexRequestBean.setRequestMethod("POST");
            weexRequestBean.setVersion("1.0");


            new SwancsHeadNetUtils.NetBuilder().setIsJsonParam(false).setGetWayParams(weexRequestBean).setParams(params).setCommonParams(MTopUtils.getCommonParams()).setNetCallback(new OkHttpClient.OkCallCancelBack() {
                @Override
                public void fail(JSONObject error) {

                    HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据新增失败");
                    ToastUtils.showShort(ReflectionApplication.getApplication(), "常用菜单数据新增失败");
                    
                }

                @Override
                public void success(JSONObject data) {

                    HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据新增成功");
                    ToastUtils.showShort(ReflectionApplication.getApplication(), "常用菜单数据新增成功");

                }
            }).build().doRequest();


        }catch (Exception e){

            e.printStackTrace();

            HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据新增失败");
            ToastUtils.showShort(ReflectionApplication.getApplication(), "常用菜单数据新增失败");
        }

    }

    //同步交换本地常用菜单

    public synchronized void swapCollectMenu(SwancsSystemCommonSimpleBean fromBean, SwancsSystemCommonSimpleBean destBean){

        try{

            int from = collectSystems.indexOf(fromBean);
            int dest = collectSystems.indexOf(destBean);

            HLogger.logWithTag("SystemMenuCustomer", "同步交换本地常用菜单成功，交换下标--from="+from+"dest="+dest);

            SwancsSystemCommonSimpleBean swancsSystemCommonSimple = collectSystems.remove(from);

            collectSystems.add(dest, swancsSystemCommonSimple);

            HLogger.logWithTag("SystemMenuCustomer", "同步交换本地常用菜单成功，数据="+JSON.toJSONString(collectSystems));

            WriteLogUtil.getInstance().saveCommonSystemMenuLog("同步交换本地常用菜单成功，数据="+JSON.toJSONString(collectSystems) + "\n\r");
        }catch (Exception e){

            e.printStackTrace();

            HLogger.logWithTag("SystemMenuCustomer", "同步交换本地常用菜单失败");
        }

    }

    //同步清空本地常用菜单
    public void clearCommonMenu(){

        try{

            if(null == collectSystems) collectSystems = new LinkedList<>();

            collectSystems.clear();

        }catch (Exception e){

            e.printStackTrace();
        }
    }

    private JSONObject changeCommonMenuObj(){

        JSONObject menuParamObj= new JSONObject();

        try{

            if(null == collectSystems || collectSystems.isEmpty()){

                menuParamObj.put("operNoTwo", (String)SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), OPER_NO, ""));
                menuParamObj.put("plist", new JSONArray());

            } else {

                menuParamObj.put("operNoTwo", (String)SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), OPER_NO, ""));

                JSONArray menuList= new JSONArray();

                for(SwancsSystemCommonSimpleBean systemMenu: collectSystems){

                    if(null!=systemMenu){

                        JSONObject menuObj = new JSONObject();

                        menuObj.put("operNo", (String)SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), OPER_NO, ""));
                        menuObj.put("menuNo", TextUtils.isEmpty(systemMenu.menuNo) ? "" :systemMenu.menuNo);
                        menuObj.put("menuName", TextUtils.isEmpty(systemMenu.menuName) ? "" :systemMenu.menuName);
                        menuObj.put("parentMenuCode", TextUtils.isEmpty(systemMenu.parentMenuCode) ? "" :systemMenu.parentMenuCode);
                        menuObj.put("menuUrl", TextUtils.isEmpty(systemMenu.menuUrl) ? "" :systemMenu.menuUrl);
                        menuObj.put("menuLevel", (0 == systemMenu.menuLevel) ? -1 : systemMenu.menuLevel);
                        menuObj.put("transType", TextUtils.isEmpty(systemMenu.transType) ? "" :systemMenu.transType);
                        menuObj.put("module", TextUtils.isEmpty(systemMenu.module) ? "" :systemMenu.module);
                        menuObj.put("menuSort", TextUtils.isEmpty(systemMenu.menuSort) ? "" :systemMenu.menuSort);

                        menuObj.put("updateTime", TimeUtil.formateTimestamp(System.currentTimeMillis(), "yyyy-MM-dd"));

                        menuObj.put("menuNum", collectSystems.indexOf(systemMenu));

                        menuList.add(menuObj);
                    }
                }


                menuParamObj.put("plist", menuList);
            }


            HLogger.logWithTag("SystemMenuCustomer", "常用菜单保存数据参数= "+JSON.toJSONString(menuParamObj));
            WriteLogUtil.getInstance().saveCommonSystemMenuLog("常用菜单保存数据参数= "+JSON.toJSONString(menuParamObj) + "\n\r");

            return menuParamObj;

        }catch (Exception e){
            e.printStackTrace();

            menuParamObj.put("operNoTwo", (String)SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), OPER_NO, ""));

            menuParamObj.put("plist", new JSONArray());

            return menuParamObj;

        }
    }

    //保存常用菜单修改
    public void saveCommonMenu(){

        try{




            WeexRequestBean weexRequestBean = new WeexRequestBean();
            weexRequestBean.setApiName("emas.swan.home.common.updateData");
            weexRequestBean.setRequestMethod("POST");
            weexRequestBean.setVersion("1.0");

            
            //解析保存后的常用菜单数据
            //jsonobject格式，{"plist": [ 菜单数据 ]}

            JSONObject menusObj = changeCommonMenuObj();

            new SwancsHeadNetUtils.NetBuilder().setIsJsonParam(true).
                    setParamsJson(menusObj)
                    .setGetWayParams(weexRequestBean)
                    .setCommonParams(MTopUtils.getCommonParams()).setNetCallback(new OkHttpClient.OkCallCancelBack() {
                @Override
                public void fail(JSONObject error) {

                    HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据保存失败");
                    ToastUtils.showShort(ReflectionApplication.getApplication(), "常用菜单数据保存失败");

                }

                @Override
                public void success(JSONObject data) {

                    HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据保存成功");
                    ToastUtils.showShort(ReflectionApplication.getApplication(), "常用菜单数据保存成功");

                }
            }).build().doRequest();

        }catch (Exception e){
            
            e.printStackTrace();

            HLogger.logWithTag("SystemMenuCustomer", "常用菜单数据保存失败");
            ToastUtils.showShort(ReflectionApplication.getApplication(), "常用菜单数据保存失败");
        }

    }

    //获取解析系统菜单-对内
    public void parseSystemMenuInner(){
        parseSystemMenu(1);
    }

    //获取解析系统菜单-对客
    public void parseSystemMenuCustom(){
        parseSystemMenu(2);
    }

    //获取解析系统菜单-对客
    private void parseSystemMenu(int menuType){

        //先从本地文件取系统菜单数据
        if(1 == menuType && null!=allInnerSwancsSystem) return;
        if(2 == menuType && null!=allCustomSwancsSystem) return;

        WriteLogUtil.getInstance().readSystemMenu(ReflectionApplication.getApplication(), menuType, new ReadSystemMenuCallback() {
            @Override
            public void readSuccess(String result) {
                if(!TextUtils.isEmpty(result)){

                    //读取成功

                    try{

                        if(1 == menuType){

                            allInnerSwancsSystem = JSON.parseObject(result, SwancsSystemBean.class);

                        } else if(2 == menuType) {

                            allCustomSwancsSystem = JSON.parseObject(result, SwancsSystemBean.class);
                        }

                    }catch (Exception e){
                        e.printStackTrace();

                        return;
                    }

                } else {
                    //读取失败, 重新获取接口请求
                    requestSystemMenu(menuType, null);
                }
            }

            @Override
            public void readFailed(String failedReason) {
                //读取失败
                //读取失败, 重新获取接口请求
                requestSystemMenu(menuType, null);

            }
        });
    }

    //重置系统菜单数据
    public void resetAllSystem(SwancsSystemBean swancsSystem){
        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            Iterator<SwancsSystemBean> it = swancsSystem.children.iterator();
            if(null != it){

                while (it.hasNext()){

                    SwancsSystemBean swancsSystemBean = it.next();

                    if(null != swancsSystemBean && swancsSystemBean.menuLevel > 1){
                        it.remove();
                    }

                }
            }

        }
    }

    private boolean isAuxiliaryMenu(){

        return TextUtils.equals("1", (String) SharedPreferencesUtil.getInstance().get(ReflectionApplication.getApplication(), ASSISTFLAG, ""));
    }

    //默认筛选菜单方式，全部收缩
    private void filterMenuDefault(SwancsSystemBean swancsSystem){

        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            for(SwancsSystemBean childMenu: swancsSystem.children){
                childMenu.menuLevel = swancsSystem.menuLevel + 1;
                childMenu.menuStatus = false;

                //是否为辅助菜单
                childMenu.isAuxiliary = isAuxiliaryMenu();

                childMenu.menuChoice = false;
                childMenu.isOpenedBottom = false;
                filterMenuDefault(childMenu);
            }
        }
    }

    //菜单全部展开方式
    private void filterMenuOpened(SwancsSystemBean swancsSystem){
        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            int size = swancsSystem.children.size();

            for(int i=0; i <size; i++){

                SwancsSystemBean childMenu = swancsSystem.children.get(i);

                //childMenu.menuLevel = swancsSystem.menuLevel + 1;
                childMenu.menuStatus = true;

                //是否为辅助菜单
                childMenu.isAuxiliary = isAuxiliaryMenu();

                childMenu.menuChoice = false;
                childMenu.isOpenedBottom = false;

                if(null!= childMenu && null!=childMenu.children && !childMenu.children.isEmpty()){

                    //添加 新数据
                    swancsSystem.children.addAll(swancsSystem.children.indexOf(childMenu)+1, childMenu.children);
                    size += childMenu.children.size();

//                    HLogger.logWithTag("SystemMenuCustomer",
//                            "菜单全部展开-----posision="
//                                    + (swancsSystem.children.indexOf(childMenu)+1)
//                                    + "内容="
//                                    +((null == childMenu.children || childMenu.children.isEmpty())
//                                    ? "空列表"
//                                    :JSON.toJSONString(childMenu.children))
//                                    + "扩展后的列表size="+size);

                    //计算一级菜单的position，除了第一个一级菜单，其余的以及菜单postion-1就是需要显示底部分割线的菜单

                    if(childMenu.menuLevel == 1 && null!=childMenu.children && !childMenu.children.isEmpty()){

                        if(TextUtils.equals(childMenu.menuCode, "90")) HLogger.logWithTag("SystemMenuCustomer", "匹配到特殊业务");

                        if(swancsSystem.children.indexOf(childMenu) - 1 > 0){

                            SwancsSystemBean bottomMenu = swancsSystem.children.get(swancsSystem.children.indexOf(childMenu) - 1);
                            if(null!=bottomMenu
                                    && ((3 == bottomMenu.menuLevel
                                    || 2 == bottomMenu.menuLevel)
                                    && !TextUtils.isEmpty(bottomMenu.menuUrl))) bottomMenu.isOpenedBottom = true;

                            HLogger.logWithTag("SystemMenuCustomer",
                                    "匹配到底部的菜单-菜单码="
                                            +bottomMenu.menuCode
                                            +"菜单父菜单="
                                            +bottomMenu.parentMenuCode
                                            +"-菜单名称="
                                            +bottomMenu.menuName
                                            +"该菜单在列表中的position="
                                            +(size - 1));
                        }
                    }
                }

                //filterMenuOpened(childMenu);
            }

            //菜单树最后一个
            SwancsSystemBean lastMenu = swancsSystem.children.get(swancsSystem.children.size() - 1);
            if(null!=lastMenu
                    && ((3 == lastMenu.menuLevel
                    || 2 == lastMenu.menuLevel)
                    && !TextUtils.isEmpty(lastMenu.menuUrl))) lastMenu.isOpenedBottom = true;
        }
    }

    //按指定菜单定位并跳转
    public void filterMenu(String menuCode, List<SwancsSystemBean> systemMenus, List<SwancsSystemBean> menuNodes) {

        if(TextUtils.isEmpty(menuCode)){
            return;
        }

        if(null == menuNodes){

            menuNodes = new LinkedList<>();
        }


        filterMenuByCode(menuCode, systemMenus, systemMenus, menuNodes);

        if(null!=menuNodes){

            Collections.reverse(menuNodes);
        }
    }

    private void filterOtherMenu(SwancsSystemBean swancsSystem, String menuCode, List<SwancsSystemBean> filterMenus){

        if(null == swancsSystem || TextUtils.isEmpty(menuCode)){

            return;
        }

        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            for(SwancsSystemBean childMenu: swancsSystem.children){

                if(null!=childMenu && TextUtils.equals(childMenu.menuCode, menuCode)){

                    if(null == filterMenus){
                        filterMenus = new ArrayList<>();
                    }
                    filterMenus.add(childMenu);
                } else {

                    filterOtherMenu(childMenu, menuCode, filterMenus);
                }
            }
        }
    }

    //获取其他系统菜单树匹配的菜单
    public SwancsSystemBean getOtherSystemMenu(int menuType, String menuCode){

        if((1!=menuType && 2!=menuType) || TextUtils.isEmpty(menuCode)){

            return null;
        }

        List<SwancsSystemBean> filterMenus = new ArrayList<>();

        if(1 == menuType){

            //获取对客菜单树
            filterOtherMenu(allCustomSwancsSystem, menuCode, filterMenus);

            if(null != filterMenus && !filterMenus.isEmpty()){
                return filterMenus.get(0);
            }

        } else if(2 == menuType) {

            //获取对内菜单树
            filterOtherMenu(allInnerSwancsSystem, menuCode, filterMenus);

            if(null != filterMenus && !filterMenus.isEmpty()){
                return filterMenus.get(0);
            }
        }

        return null;

    }

    public void filterMenuByCode(String menuCode, List<SwancsSystemBean> systemMenus, List<SwancsSystemBean> oldSystemMenus,  List<SwancsSystemBean> menuNodes){

        //HLogger.logWithTag("MenuModuleView", "filterMenuByCode开始执行");

        if(null!=systemMenus
                && !systemMenus.isEmpty()){

            for(SwancsSystemBean childMenu: systemMenus){
                if(null!=childMenu){

                    if(TextUtils.equals(childMenu.menuCode, menuCode)){

                        if(null == menuNodes){

                            menuNodes = new LinkedList<>();
                        }

                        menuNodes.add(childMenu);

                        if(!TextUtils.isEmpty(childMenu.parentMenuCode) && !TextUtils.equals(childMenu.parentMenuCode, "10")){

                            filterMenuByCode(childMenu.parentMenuCode, oldSystemMenus, oldSystemMenus, menuNodes);
                        }

                    } else {

                        filterMenuByCode(menuCode, childMenu.children, oldSystemMenus, menuNodes);
                    }
                }
            }
        }

        //HLogger.logWithTag("MenuModuleView", "filterMenuByCode结束执行");

    }
    
    private void clearBottomTag(SwancsSystemBean swancsSystem){
        if(null!=swancsSystem){

            swancsSystem.isOpenedBottom = false;

            if(null!=swancsSystem.children && !swancsSystem.children.isEmpty()){

                for(SwancsSystemBean childMenu: swancsSystem.children){
                    childMenu.isOpenedBottom = false;
                    clearBottomTag(childMenu);
                }
            }
        }
    }

    public int getBottomMenu(List<SwancsSystemBean> systemMenus){

        if(null == systemMenus || systemMenus.isEmpty()){
            return -1;
        }


        for(SwancsSystemBean subSystemMenu : systemMenus){

            //先清除底部线显示标识
            clearBottomTag(subSystemMenu);

            if(null!=subSystemMenu
                    && 1 == subSystemMenu.menuLevel
                    && subSystemMenu.menuStatus
                    && null!=subSystemMenu.children
                    && !subSystemMenu.children.isEmpty()){

                //如果第一级别被打开，则判断最后一个子菜单有没有打开
                SwancsSystemBean subSystemMenuBean = subSystemMenu.children.get(subSystemMenu.children.size() - 1);

                if(null!=subSystemMenuBean
                        && subSystemMenuBean.menuStatus
                        && null != subSystemMenuBean.children
                        && !subSystemMenuBean.children.isEmpty()){

                    //返回最后一个菜单

                    SwancsSystemBean resultSystemMenu = subSystemMenuBean.children.get(subSystemMenuBean.children.size() - 1);

                    if(null!=resultSystemMenu){
                        resultSystemMenu.isOpenedBottom = true;

                        int position = systemMenus.indexOf(resultSystemMenu);

                        HLogger.logWithTag("SwancsSystemAdapter",
                                "计算底部菜单-菜单码="
                                        +resultSystemMenu.menuCode
                                        +"菜单父菜单="
                                        +resultSystemMenu.parentMenuCode
                                        +"-菜单名称="
                                        +resultSystemMenu.menuName
                                        +"该菜单在列表中的position="
                                        +position);

                        return position;
                    } else {
                        return -1;
                    }

                } else if(null!=subSystemMenuBean){

                    subSystemMenuBean.isOpenedBottom = true;

                    int position = systemMenus.indexOf(subSystemMenuBean);

                    HLogger.logWithTag("SwancsSystemAdapter",
                            "计算底部菜单-菜单码="
                                    +subSystemMenuBean.menuCode
                                    +"菜单父菜单="
                                    +subSystemMenuBean.parentMenuCode
                                    +"-菜单名称="
                                    +subSystemMenuBean.menuName
                                    +"该菜单在列表中的position="
                                    +position);

                    return position;

                } else {
                    return -1;
                }
            }
        }

        return -1;
    }

    public int getChoiceMenuPosition(SwancsSystemBean swancsSystem, List<SwancsSystemBean> systemMenus){

        if(null == swancsSystem || null == systemMenus || systemMenus.isEmpty()){
            return -1;
        }

        String parentMenuCode = swancsSystem.parentMenuCode;

        SwancsSystemBean swancsSystemParent = getParentMenu(parentMenuCode, systemMenus);

        if(null == swancsSystemParent
                || null == swancsSystemParent.children
                || swancsSystemParent.children.isEmpty()
                || !swancsSystemParent.menuStatus){
            return -1;
        }

        for(SwancsSystemBean subSystemMenu : swancsSystemParent.children){

            if(subSystemMenu.menuChoice){

                subSystemMenu.menuChoice = false;

                return systemMenus.indexOf(subSystemMenu);
            }
        }

        return -1;

    }

    public SwancsSystemBean getMenuOpened(List<SwancsSystemBean> systemMenus, SwancsSystemBean currentSystemMenu){

        if(null == systemMenus || systemMenus.isEmpty() || null == currentSystemMenu){
            return null;
        }

        for(SwancsSystemBean subMenu : systemMenus){

            if(1 == subMenu.menuLevel
                    && null!=subMenu.children
                    && !subMenu.children.isEmpty() && subMenu.menuStatus){
                //如果当前的菜单是父菜单，则不做关闭操作
                if(TextUtils.equals(subMenu.menuCode, currentSystemMenu.menuCode)
                        || TextUtils.equals(subMenu.menuCode, currentSystemMenu.parentMenuCode)){
                    continue;
                }

                //subMenu.menuStatus = false;
                return subMenu;
            } else if(2 == subMenu.menuLevel
                    && null!=subMenu.children
                    && !subMenu.children.isEmpty() && subMenu.menuStatus){
                //subMenu.menuStatus = false;

                if(TextUtils.equals(subMenu.menuCode, currentSystemMenu.menuCode)
                        || TextUtils.equals(subMenu.menuCode, currentSystemMenu.parentMenuCode)){
                    continue;
                }

                return subMenu;
            }
        }

        return null;
    }

    public SwancsSystemBean getParentMenu(String parentMenuCode, List<SwancsSystemBean> systemMenus){

        if(TextUtils.isEmpty(parentMenuCode) || null == systemMenus || systemMenus.isEmpty()){
            return null;
        }

        for(SwancsSystemBean swancsSystem : systemMenus){

            if(null!=swancsSystem && TextUtils.equals(parentMenuCode, swancsSystem.menuCode)){

                return swancsSystem;
            }
        }

        return null;

    }

    //获取对客系统菜单数据
    public List<SwancsSystemBean> getSystemCustomMenus(boolean isAllExpand){


        if(null == allCustomSwancsSystem){
            return null;
        }

        if(isAllExpand){

            //全部展开

            if(null != allCustomSwancsSystem){
                allCustomSwancsSystem.menuLevel = 0;
                allCustomSwancsSystem.menuStatus = false;

                //是否为辅助菜单
                allCustomSwancsSystem.isAuxiliary = isAuxiliaryMenu();

                allCustomSwancsSystem.menuChoice = false;
                allCustomSwancsSystem.isOpenedBottom = false;

                resetAllSystem(allCustomSwancsSystem);
                filterMenuOpened(allCustomSwancsSystem);
                return allCustomSwancsSystem.children;
            }else {

                return null;
            }


        } else {

            if(null != allCustomSwancsSystem){
                allCustomSwancsSystem.menuLevel = 0;
                allCustomSwancsSystem.menuStatus = false;

                //是否为辅助菜单
                allCustomSwancsSystem.isAuxiliary = isAuxiliaryMenu();

                allCustomSwancsSystem.menuChoice = false;
                allCustomSwancsSystem.isOpenedBottom = false;

                resetAllSystem(allCustomSwancsSystem);

                filterMenuDefault(allCustomSwancsSystem);
                return allCustomSwancsSystem.children;
            }else {

                return null;
            }
        }
    }

    //获取对内系统菜单数据
    public List<SwancsSystemBean> getSystemInnerMenus(boolean isAllExpand){


        if(null == allInnerSwancsSystem){
            return null;
        }

        if(isAllExpand){

            //全部展开

            if(null != allInnerSwancsSystem){
                allInnerSwancsSystem.menuLevel = 0;
                allInnerSwancsSystem.menuStatus = false;

                //是否为辅助菜单
                allInnerSwancsSystem.isAuxiliary = isAuxiliaryMenu();

                allInnerSwancsSystem.menuChoice = false;
                allInnerSwancsSystem.isOpenedBottom = false;

                resetAllSystem(allInnerSwancsSystem);
                filterMenuOpened(allInnerSwancsSystem);
                return allInnerSwancsSystem.children;
            }else {

                return null;
            }


        } else {

            if(null != allInnerSwancsSystem){
                allInnerSwancsSystem.menuLevel = 0;
                allInnerSwancsSystem.menuStatus = false;

                //是否为辅助菜单
                allInnerSwancsSystem.isAuxiliary = isAuxiliaryMenu();

                allInnerSwancsSystem.menuChoice = false;
                allInnerSwancsSystem.isOpenedBottom = false;

                resetAllSystem(allInnerSwancsSystem);

                filterMenuDefault(allInnerSwancsSystem);
                return allInnerSwancsSystem.children;
            }else {

                return null;
            }
        }
    }

    private boolean commonExixts(SwancsSystemCommonSimpleBean swancsSystem){

        if(null == swancsSystem) return true;

        if(null == collectSystems){
            collectSystems = new LinkedList<>();

            return false;
        } else if(collectSystems.isEmpty()){

            return false;
        }


        Iterator<SwancsSystemCommonSimpleBean> it = collectSystems.iterator();

        if(null!=it){

            while (it.hasNext()){

                SwancsSystemCommonSimpleBean sunMenu = it.next();
                if(null!=sunMenu && TextUtils.equals(sunMenu.menuNo, swancsSystem.menuNo)){

//                    it.remove();
//                    swancsSystemMenuView.autoDeleteNormal(sunMenu);
                    return true;
                }
            }

            return false;
        }



        return true;





    }

    public void putCollectMenu(SwancsSystemCommonSimpleBean swancsSystem){
        if(null == swancsSystem){
            return;
        }

        if(null == collectSystems){
            collectSystems = new LinkedList<>();
        }

        //判断当前新增的常用菜单是否已经在常用菜单数据集合中存在

        if(commonExixts(swancsSystem)){


            ToastUtils.showShort(ReflectionApplication.getApplication(), "菜单已添加到常用菜单列表");

            return;

        } else if(collectSystems.size() >= SwancsSystemNormalAdapter.MAX_COUNT){

            ToastUtils.showShort(ReflectionApplication.getApplication(), "常用菜单最多可添加30个");

            return;

        }else {

            swancsSystemMenuView = getSystemMenuCompontent();

            swancsSystem.isEditor = swancsSystemMenuView.isEditorView();

            collectSystems.add(swancsSystem);

            //如果当前是常用菜单视图，需要自动增加
            swancsSystemMenuView.autoAddNormal(swancsSystem);

            //调用新增接口

            addNormalMenu(swancsSystem);

        }
    }

    public List<SwancsSystemCommonSimpleBean> getCollectMenu(){

        try{

            return collectSystems;


        }catch (Exception e){

            e.printStackTrace();

            return null;
        }
    }

    public void removeCollectMenu(SwancsSystemCommonSimpleBean swancsSystemBean){

        if(null == collectSystems || collectSystems.isEmpty() || null == swancsSystemBean) return;

        collectSystems.remove(swancsSystemBean);
    }

    //二次删除无效父类菜单

    private void deleteInvalidParentMenu(SwancsSystemBean swancsSystem, List<String> shouldDeleteMenus){

        if(null == swancsSystem || null==shouldDeleteMenus || shouldDeleteMenus.isEmpty()){

            return;
        }

        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            Iterator<SwancsSystemBean> it = swancsSystem.children.iterator();

            if(null!=it){

                while (it.hasNext()){

                    SwancsSystemBean childMenu = it.next();

                    if(null!=childMenu
                            && shouldDeleteMenus.contains(childMenu.menuCode)){
                        it.remove();

                        HLogger.logWithTag("deleteInvalidMenu", "父菜单菜单="+childMenu.menuName+"，已删除");

                        WriteLogUtil.getInstance().saveSearchMenu("deleteInvalidParentMenu--父菜单菜单="+childMenu.menuName+"，已删除");

                    } else {

                        deleteInvalidParentMenu(childMenu, shouldDeleteMenus);
                    }

                }
            }
        }
    }
    
    //去掉没有自己点，并且没有menuurl的菜单
    private void deleteInvalidMenu(SwancsSystemBean swancsSystem, List<String> shouldDeleteMenus){

        if(null == swancsSystem){

            return;
        }

        if(null == shouldDeleteMenus){
            shouldDeleteMenus = new ArrayList<>();
        }

        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            Iterator<SwancsSystemBean> it = swancsSystem.children.iterator();

            if(null!=it){

                while (it.hasNext()){

                    SwancsSystemBean childMenu = it.next();

                    if(null!=childMenu
                            && (null == childMenu.children || childMenu.children.isEmpty()) && (TextUtils.isEmpty(childMenu.menuUrl) || TextUtils.isEmpty(childMenu.module) )){
                        it.remove();

                        HLogger.logWithTag("deleteInvalidMenu", "子菜单菜单="+childMenu.menuName+"，已删除");

                        WriteLogUtil.getInstance().saveSearchMenu("deleteInvalidMenu--子菜单菜单="+childMenu.menuName+"，已删除");

                    } else {

                        deleteInvalidMenu(childMenu, shouldDeleteMenus);
                    }

                }

                if(null!=swancsSystem && swancsSystem.children.isEmpty()){

                    HLogger.logWithTag("deleteInvalidMenu", "当前菜单="+swancsSystem.menuName+"，子菜单为空");

                    //删除该菜单对象
                    shouldDeleteMenus.add(swancsSystem.menuCode);

                }
            }
        }

        HLogger.logWithTag("deleteInvalidMenu", "向上删除父菜单的数量="+((null==shouldDeleteMenus || shouldDeleteMenus.isEmpty())?"0":shouldDeleteMenus.size()));
    }

    private void parseSimpleSystemMenu(SwancsSystemBean swancsSystem){

        if(null == swancsSystem){

            WriteLogUtil.getInstance().saveSearchMenu("菜单搜索-解析数据为空-返回");

            return;
        }

        if(null == simpleSystemMenus){

            simpleSystemMenus = new LinkedList<>();
        }

        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            for(SwancsSystemBean childMenu: swancsSystem.children){

                if(null!=childMenu && !TextUtils.isEmpty(childMenu.menuUrl)){

                    SimpleSystemMenuBean simpleSystemMenu = new SimpleSystemMenuBean();
                    simpleSystemMenu.key = childMenu.menuCode;
                    simpleSystemMenu.label = childMenu.menuName;

//                    if(simpleSystemMenus.isEmpty()){
//
//                        simpleSystemMenus.add(simpleSystemMenu);
//                    } else {
//
//                        Iterator<SimpleSystemMenuBean> it = simpleSystemMenus.iterator();
//
//                        while (it.hasNext()){
//
//                            SimpleSystemMenuBean simpleSysteExists = it.next();
//
//                            if(null!=simpleSysteExists && TextUtils.equals(simpleSysteExists.key, simpleSystemMenu.key)){
//
//                                it.remove();
//                            }
//                        }
//
//                        //WriteLogUtil.getInstance().saveSearchMenu("菜单搜索-解析到搜索菜单="+simpleSystemMenu.key);
//
//                        simpleSystemMenus.add(simpleSystemMenu);
//
//                    }

                    simpleSystemMenus.add(simpleSystemMenu);
                }

                parseSimpleSystemMenu(childMenu);
            }
        }

    }

    public static interface ReadSystemMenuCallback{

        void readSuccess(String result);

        void readFailed(String failedReason);

    }

    public static interface RequestSysmenuCompleteCallback{

        void loadComplete(int menuType);

        void loadSimpleSystemMenu(String simpleMenuJson);
    }

    //根据菜单url获取对应的菜单
    public SwancsSystemBean searchMenuByMenuUrl(String menuUrl){

        if(TextUtils.isEmpty(menuUrl)){

            return null;
        }

        List<SwancsSystemBean> results = new ArrayList<>();

        searchMenuByMenuUrl(menuUrl, allCustomSwancsSystem, results);
        if(null != results && !results.isEmpty()){
            return results.get(0);
        }else {
            searchMenuByMenuUrl(menuUrl, allInnerSwancsSystem, results);
        }

        if(null!=results && !results.isEmpty()) return results.get(0);
        return null;
    }

    //根据menucode查询系统菜单
    public SwancsSystemBean searchMenuByCode(String menuCode){

        if(TextUtils.isEmpty(menuCode)){

            return null;
        }

        List<SwancsSystemBean> results = new ArrayList<>();

        searchMenuByCode(menuCode, allCustomSwancsSystem, results);
        if(null != results && !results.isEmpty()){
            return results.get(0);
        }else {
            searchMenuByCode(menuCode, allInnerSwancsSystem, results);
        }

        if(null!=results && !results.isEmpty()) return results.get(0);
        return null;
    }

    public void searchMenuByMenuUrl(String menuUrl, SwancsSystemBean swancsSystem, List<SwancsSystemBean> results){

        if(TextUtils.isEmpty(menuUrl) || null==swancsSystem){

            return;
        }

        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            for(SwancsSystemBean childMenu: swancsSystem.children){

                String menuUrlTemp = null;
                if(null!=childMenu) menuUrlTemp = childMenu.menuUrl;

                if(menuUrl.contains("\\\\")){
                    menuUrl = menuUrl.replace("\\\\", "\\");
                }

                if(menuUrl.contains("\r\n")){
                    menuUrl = menuUrl.replaceAll("\r\n", "");
                }

                if(!TextUtils.isEmpty(menuUrlTemp)
                        && TextUtils.equals(menuUrlTemp.replaceAll("\r\n", ""), menuUrl)){

                    if(null == results) results = new ArrayList<>();
                    results.add(childMenu);
                } else {
                    searchMenuByMenuUrl(menuUrl, childMenu, results);
                }
            }
        }


    }

    public void searchMenuByCode(String menuCode, SwancsSystemBean swancsSystem, List<SwancsSystemBean> results){

        if(TextUtils.isEmpty(menuCode) || null==swancsSystem){

            return;
        }

        if(null!=swancsSystem
                && null!=swancsSystem.children
                && !swancsSystem.children.isEmpty()){

            for(SwancsSystemBean childMenu: swancsSystem.children){

                if(null!=childMenu && TextUtils.equals(childMenu.menuCode, menuCode)){

                    if(null == results) results = new ArrayList<>();
                    results.add(childMenu);
                } else {
                    searchMenuByCode(menuCode, childMenu, results);
                }
            }
        }
    }

    public boolean menuInnerReady(){

        return innerMenuCanUsed;

    }

    public boolean menuCommonReady(){

        return commonMenuCanUsed;

    }

    private WxSwancsSystemMenuView getSystemMenuCompontent(){

        if(null == swancsSystemMenuView){

            swancsSystemMenuView = ReflectUtils.getComponent(WxSwancsSystemMenuView.mInstanceId, WxSwancsSystemMenuView.mRef);
        }

        return swancsSystemMenuView;
    }

}
